#!/bin/bash
clear

TEXT='\033[30;107;2m'
INFO='\033[97;44m'
WARNING='\033[97;43m'
ERROR='\033[97;41m'
SUCCESS='\033[97;42m'
GREEN='\033[92m'
YELLOW='\033[93m'
RESET='\033[0m'

if [ "${EUID}" -ne 0 ]; then
  echo -e "${ERROR} RALAT ${RESET}${TEXT} Anda tiada kebenaran untuk menjalankan skrip ini! ${RESET}"
  exit 1
fi

function installPkg {
  read -p "Teruskan dengan pemasangan semula? [Y/n] " _yesno
  if [[ "$_yesno" =~ ^[Yy]$ ]]; then
    if [[ ! -d /etc/shadowsocks-libev ]]; then
      wget -q -O - https://raw.githubusercontent.com/cybertize/debian/buster/packages/sslibev.sh | bash
    fi
    apt-get -y -qq --purge remove shadowsocks-libev &>/dev/null
    apt-get -y -qq autoremove &>/dev/null
    apt-get -qq autoclean &>/dev/null
    clear
    sleep 3
    wget -q -O - https://raw.githubusercontent.com/cybertize/debian/buster/packages/sslibev.sh | bash
  else
    echo -e "${ERROR} INFO ${RESET}${TEXT} Membatalkan pemasangan semula ${RESET}"
    exit 1
  fi
}

function uninstallPkg {
  clear
  read -p "Adakah anda pasti mahu menyahpasang shadowsocks-libev? [Y/n] " _yesno
  if [[ "$_yesno" =~ ^[Yy]$ ]]; then
    apt-get -y -qq --purge remove stunnel &>/dev/null
    apt-get -y -qq autoremove &>/dev/null
    apt-get -y -qq autoclean &>/dev/null

    echo -e "${SUCCESS} INFO ${RESET}${TEXT} Bersihkan & Keluarkan pakej shadowsocks-libev ${RESET}"
    exit 0
  else
    echo -e "${ERROR} INFO ${RESET}${TEXT} Batalkan penyahpasangan pakej ${RESET}"
    exit 1
  fi
}

function configurePkg {
  server_default() {
    local default_port=$(cat /etc/shadowsocks-libev/server-default.json | grep -w '"server_port"' | cut -d ':' -f '2' | tr -d ',')

    until [[ -n "$new_default_port" && "$new_default_port" =~ ^[0-9]+$ ]]; do
      read -p "Masukkan port baru: " new_default_port
      sed -i "s|$default_port|$new_default_port|g" /etc/shadowsocks-libev/server-default.json
      systemctl restart shadowsocks-libev-server@server-default
    done
  }
  server_stunnel() {
    local stunnel_port=$(cat /etc/shadowsocks-libev/server-stunnel.json | grep -w '"server_port"' | cut -d ':' -f '2' | tr -d ',')

    until [[ -n "$new_stunnel_port" && "$new_stunnel_port" =~ ^[0-9]+$ ]]; do
      read -p "Masukkan port baru: " new_stunnel_port
      sed -i "s|$stunnel_port|$new_stunnel_port|g" /etc/shadowsocks-libev/server-stunnel.json
      sed -i "s|connect = 127.0.0.1:$stunnel_port|connect = 127.0.0.1:$new_stunnel_port|g" /etc/stunnel/stunnel.conf
      systemctl restart shadowsocks-libev-server@server-stunnel
      systemctl restart stunnel4
    done
  }

  echo
  echo -e "${TEXT} MENU FAIL KONFIG SHADOWSOCKS-LIBEV                        ${RESET}"
  echo
  echo -e "[01] ${GREEN}default${RESET}   - ${YELLOW}/etc/openvpn/server-tcp.conf${RESET}"
  echo -e "[02] ${GREEN}stunnel${RESET}   - ${YELLOW}/etc/openvpn/server-udp.conf${RESET}"
  echo -e "[03] ${GREEN}back${RESET}  - ${YELLOW}Exit from config file menu${RESET}"
  echo -e "[00] ${GREEN}quit${RESET}  - ${YELLOW}Exit from config file menu${RESET}"
  echo
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
  while true; do
    read -p "Konfigurasi yang anda mahu edit: " _choice
    case "$_choice" in
    01 | default)
      server-default && break
      ;;
    02 | stunnel)
      server-stunnel && break
      ;;
    03 | back)
      configurePkg && break
      ;;
    00 | quit)
      exit 0
      ;;
    *)
      echo
      echo -e "${ERROR} INFO ${RESET}${TEXT} Dibatalkan ${RESET}"
      echo
      configurePkg
      ;;
    esac
  done
}

function detailPkg {
  local unit_id=$(systemctl show shadowsocks-libev.service -p Id | cut -d '=' -f 2)
  local unit_name=$(systemctl show shadowsocks-libev.service -p Names | cut -d '=' -f 2)
  local unit_desc=$(systemctl show shadowsocks-libev.service -p Description | cut -d '=' -f 2)
  local is_active_default=$(systemctl is-active shadowsocks-libev-server@server-default.service)
  local is_active_stunnel=$(systemctl is-active shadowsocks-libev-server@server-default.service)
  local is_enable_default=$(systemctl is-enabled shadowsocks-libev-server@server-default.service)
  local is_enable_stunnel=$(systemctl is-enabled shadowsocks-libev-server@server-default.service)

  local default_port=$(cat /etc/shadowsocks-libev/server-default.json | grep -w '"server_port"' | cut -d ':' -f 2 | tr ',' ' ')
  local stunnel_port=$(cat /etc/shadowsocks-libev/server-stunnel.json | grep -w '"server_port"' | cut -d ':' -f 2 | tr ',' ' ')

  echo
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo -e "${TEXT} SHADOWSOCKS-LIBEV service detail                           ${RESET}"
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo
  echo -e "${YELLOW}          Unit ID:${RESET} ${GREEN}$unit_id${RESET}"
  echo -e "${YELLOW}        Unit Name:${RESET} ${GREEN}$unit_name${RESET}"
  echo -e "${YELLOW}  Unit Desciption:${RESET} ${GREEN}$unit_desc${RESET}"
  echo -e "${YELLOW} Status [Default]:${RESET} ${GREEN}$is_active_default & $is_enable_default${RESET}"
  echo -e "${YELLOW} Status [Stunnel]:${RESET} ${GREEN}$is_active_stunnel & $is_enable_stunnel${RESET}"
  echo -e "${YELLOW}       Port Lists:${RESET} ${GREEN}$default_port[Default], $stunnel_port[Stunnel]${RESET}"
  echo
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
}

function libevMenu {
  echo
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo -e "${TEXT} MENU SHADOWSOCKS-LIBEV                                     ${RESET}"
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo
  echo -e "[01] ${GREEN}reinstall${RESET}    - ${YELLOW}Reinstall shadowsocks package${RESET}"
  echo -e "[02] ${GREEN}uninstall${RESET}    - ${YELLOW}Uninstall shadowsocks package${RESET}"
  echo -e "[03] ${GREEN}configure${RESET}    - ${YELLOW}Edit/Configure shadowsocks file${RESET}"
  echo -e "[04] ${GREEN}detail   ${RESET}    - ${YELLOW}Show shadowsocks detail & status${RESET}"
  echo -e "[00] ${GREEN}quit     ${RESET}    - ${YELLOW}Exit from menu${RESET}"
  echo
  echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
  echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
  echo -e "${TEXT} ========================================================== ${RESET}"
  echo
  while true; do
    read -p "Masukkan pilihan anda: " _choice
    case "$_choice" in
    01 | reinstall)
      installPkg && break
      ;;
    02 | uninstall)
      uninstallPkg && break
      ;;
    03 | configure)
      configurePkg && break
      ;;
    04 | detail)
      detailPkg && break
      ;;
    00 | quit)
      exit 0
      ;;
    *)
      echo -e "${ERROR} RALAT ${RESET}${TEXT} Pilihan tidak sah ${RESET}"
      libevMenu
      ;;
    esac
  done
}

if [[ -d /etc/shadowsocks-libev ]]; then
  libevMenu
elif [[ ! -d /etc/shadowsocks-libev ]]; then
  installPkg
fi
